package cn.stronglink.assetmanage.business.check.presenter;

import android.graphics.Bitmap;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.model.CheckDetailModel;
import cn.stronglink.assetmanage.business.check.view.detail.CheckDetailView;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.ModelDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Brand;
import cn.stronglink.assetmanage.entity.Model;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.entity.Type;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 入库验收详情页逻辑
 * Created by chencongcong on 2018/6/15.
 */

public class CheckDetailPresenter implements CheckDetailModel.onCheckDetailListener{
    private CheckDetailModel checkDetailModel;
    private CheckDetailView checkDetailView;

    public CheckDetailPresenter(CheckDetailModel checkDetailModel, CheckDetailView checkDetailView) {
        this.checkDetailModel = checkDetailModel;
        this.checkDetailView = checkDetailView;
        this.checkDetailModel.setCheckDetailListener(this);
    }

    public void setSpinnerData() {
        String[] roomArr = checkDetailModel.getRoomArr();
        String[] brandArr = checkDetailModel.getBrandArr();
        String[] typeArr = checkDetailModel.getAssetTypeArr();
        checkDetailView.setSpinnerData(roomArr,brandArr,typeArr);
    }


    /**
     * 显示资产数据
     * @param asset_id
     */
    public void showDetailData(long asset_id) {
        Asset asset = checkDetailModel.getAssetById(asset_id);
        if (asset != null) {
            String url = asset.getPictureUri();
            Bitmap bitmap = null;
            if(url!=null&&!url.isEmpty()){
                try {
                    bitmap =checkDetailModel.getBitmapByUrl(url);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            checkDetailView.showAssetDetail(asset,bitmap);
        }
    }

    /**
     * 显示资产数据
     */
    public void showDetailData(Asset asset) {
        if (asset != null) {
            String url = asset.getPictureUri();
            Bitmap bitmap = null;
            if(url!=null&&!url.isEmpty()){
                try {
                    bitmap =checkDetailModel.getBitmapByUrl(url);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            checkDetailView.showAssetDetail(asset,bitmap);
        }
    }

    /**
     *保存数据
     * @param asset
     */
    public void saveDetailData(Asset asset){
        checkDetailModel.saveCheckData(asset);
    }


    @Override
    public void onFailure(Call call, IOException e) {
        checkDetailView.hideProgressDialog();
        checkDetailView.showToast(e.getMessage());
    }

    @Override
    public void onResponse(Call call, Response response) {
        checkDetailView.hideProgressDialog();
        if(response.isSuccessful()){
            if(response.code()==200){
                //删除本地数据
                checkDetailModel.deleteSyncData();
            }else{
                checkDetailView.showToast(response.message());
            }
        }else{
            checkDetailView.showToast(MyApplication.APP_CONTEXT.getString(R.string.get_data_fail));
        }
    }

    public List<Type> getTypeList() {
        List<Type> typeList = new ArrayList<>();
        try {
            Type type = new Type();
            type.setId_("");
            type.setName("请选择类型");
            typeList.add(type);

            List<Type> types = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().loadAll();
            if (types != null && types.size() > 0) {
                typeList.addAll(types);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return typeList;
    }

    public List<Brand> getBrandList() {
        List<Brand> brandList = new ArrayList<>();
        try {
            Brand brand = new Brand();
            brand.setId_("");
            brand.setName("请选择品牌");
            brandList.add(brand);
            List<Brand> brands = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().loadAll();
            if (brands != null && brands.size() > 0) {
                brandList.addAll(brands);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return brandList;
    }

    public List<Model> getModelList() {
        List<Model> modelList = new ArrayList<>();
        Model model = new Model();
        model.setBrandId_("");
        model.setTypeId_("");
        model.setId_("");
        model.setName("请选择型号");
        modelList.add(model);
        List<Model> models = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getModelDao().loadAll();
        if (models!=null&&models.size()>0){
            modelList.addAll(models);
        }
        return modelList;
    }

    public List<Room> getRoomList() {
        List<Room> roomList = new ArrayList<>();
        try {
            Room room = new Room();
            room.setId_("");
            room.setName("请选择地点");
            roomList.add(room);
            List<Room> rooms = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().loadAll();
            if (rooms != null && rooms.size() > 0) {
                roomList.addAll(rooms);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return roomList;
    }

    public List<Model> queryModelByBrandAndType(String brandId,String typeId) {
        List<Model> modelList = new ArrayList<>();
        try {
            Model model = new Model();
            model.setBrandId_("");
            model.setTypeId_("");
            model.setId_("");
            model.setName("请选择型号");
            modelList.add(model);
            List<Model> models = DaoManager.getDaoSession(MyApplication.APP_CONTEXT)
                    .getModelDao().queryBuilder().where(
                            ModelDao.Properties.BrandId_.eq(brandId),
                            ModelDao.Properties.TypeId_.eq(typeId)).list();
            if (models != null) {
                modelList.addAll(models);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return modelList;
    }
}
